﻿using Acme.BookStore.Enum;
using Acme.BookStore.RBAC;
using Acme.BookStore.RoomNos;
using Microsoft.AspNetCore.Mvc;
using SC.UserCenter.Infra;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace Acme.BookStore.Lodgings
{
    /// <summary>
    ///住宿记录实现
    /// </summary>
    [ApiExplorerSettings(GroupName ="v2")]
    public class LodgingServices : ApplicationService, ILodgingServices
    {
        /// <summary>
        ///注入
        /// </summary>
        private readonly IRepository<Lodging> repository;

        IRepository<Reservations> _Reservationsrepository;

        IRepository<User> _UserReservationsrepository;

        IRepository<RoomNumber> _RoomNumberReservationsrepository;

        IRepository<RoomType> _RoomTypeReservationsrepository;

        public LodgingServices(IRepository<Lodging> repository, IRepository<Reservations> reservationsrepository, IRepository<User> userReservationsrepository, IRepository<RoomNumber> roomNumberReservationsrepository, IRepository<RoomType> roomTypeReservationsrepository)
        {
            this.repository = repository;
            _Reservationsrepository = reservationsrepository;
            _UserReservationsrepository = userReservationsrepository;
            _RoomNumberReservationsrepository = roomNumberReservationsrepository;
            _RoomTypeReservationsrepository = roomTypeReservationsrepository;
        }

        /// <summary>
        /// 住宿记录数据添加
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpPost("LodgingAdd")]
        public async Task<ApiResult> LodgingAdd(LodgingDTO dto)
        {
            var list = ObjectMapper.Map<LodgingDTO, Lodging>(dto);
            await repository.InsertAsync(list);
            return ApiResult.Ok(list);
        }

        /// <summary>
        /// 住宿记录显示
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpGet("LodgingShow")]
        public async Task<ApiResult> LodgingShow(int PageIndex, int PageSize, string? UserName, DateTime? CheckIntime, DateTime? CheckOuttime, StayStateEnum? LodgingStatus)
        {
            var user = await _UserReservationsrepository.GetListAsync();
            var roomNumber = await _RoomNumberReservationsrepository.GetListAsync();
            var roomType = await _RoomTypeReservationsrepository.GetListAsync();
            var reservations = await _Reservationsrepository.GetListAsync();
            var lodging = await repository.GetListAsync();

            
            var list = from a in user
                       join b in reservations on a.Id equals b.UserId
                       join c in roomType on b.RoomTypeId equals c.Id
                       join d in roomNumber on b.RoomId equals d.Id
                       join e in lodging on a.Id equals e.UserId
                       select new LodgingShowDTO()
                       {

                           UserName = a.UserName,
                           UserPwd = a.UserPwd,
                           UserNickName = a.UserNickName,
                           UserErrorCount = a.UserErrorCount,
                           UserIsLock = a.UserIsLock,
                           User_Phone = a.User_Phone,
                           User_Email = a.User_Email,
                           User_Role = a.User_Role,
                           User_Points = a.User_Points,
                           UserId = a.Id,
                           CheckIntime = b.CheckIntime,
                           CheckOuttime = b.CheckOuttime,
                           OutofPocketamount = b.OutofPocketamount,
                           RoomTypeId = c.Id,
                           RoomName=c.RoomName,
                           OrderType = b.OrderType,
                           FullStatus = b.FullStatus,
                           PaidStatus = b.PaidStatus,
                           RoomId = d.Id,
                           RoomNO=d.RoomNO,
                           ReservationsId = b.Id,
                           LodgingId = e.Id,
                           LodgingStatus = e.LodgingStatus,
                           UserStyle = e.UserStyle

                       };
            //根据名称模糊查询
            if(!string.IsNullOrEmpty(UserName))
            {
                list = list.Where(x => x.UserName.Contains(UserName));
            }
            //根据状态查询
            if(LodgingStatus!=null)
            {
                list = list.Where(x => x.LodgingStatus == LodgingStatus);
            }
            //根据时间查询
            if (CheckIntime != null && CheckOuttime == null)
            {
                list = list.Where(x => x.CheckIntime >= CheckIntime);
            }
            if (CheckOuttime != null && CheckIntime == null)
            {
                list = list.Where(x => x.CheckOuttime <= CheckOuttime);
            }

            if (CheckIntime != null && CheckOuttime != null)
            {
                list = list.Where(x => CheckIntime >= CheckIntime && x.CheckOuttime <= CheckOuttime);
            }
            //分页
            var page = new APiPaging()
            {
                TotalCount = list.Count(),
                TotalPage = Convert.ToInt32(Math.Ceiling(list.Count() / Convert.ToDouble(PageSize))),
                Data = list.OrderBy(x => x.CheckIntime).Skip((PageIndex - 1) * PageSize).Take(PageSize).ToList()
            };
            return ApiResult.Ok(page);
        }

        /// <summary>
        /// 删除住宿记录
        /// </summary>
        /// <param name="Id">住宿编号</param>
        /// <returns></returns>
        [HttpDelete("DeleteLodging")]
        public async Task<ApiResult> DeleteLodgingAsync(Guid LodgingId)
        {
            try
            {
                await repository.DeleteAsync(x => x.Id == LodgingId);

                
                return ApiResult.Ok();

            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
